/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-11-26
**	Function List： 
**  History:         
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	防止重复包含
*********************************************************************************************************
*/

#ifndef _CAN_MOTO_PROTOCOL_H_
#define _CAN_MOTO_PROTOCOL_H_
/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/



/*
*********************************************************************************************************
*                                              	头文件
*********************************************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include "can_protocol.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
#define BOARD_MOTOR_MAX  31
#define MOTOR_INDEX_TO_SDO_ADDR(channel)			(MOTOR_ADDR_BASE + channel)
#define SDO_ADDR_TO_MOTOR_INDEX(sdo_addr)			(sdo_addr - MOTOR_ADDR_BASE)
#define MOTOR_RPM_TO_DATA(rpm)								(895 * rpm)

#define MOTOR_ADDR_BASE					0x50

#define M_STATUS_SPI_ERR		0x00001000 /*写入TMC寄存器错误*/
#define M_STATUS_SPEED_ERR		0x00000800 /*电机莫名位置模式低速的错误*/
#define M_STATUS_SPI_error      0x00000400
#define M_STATUS_HOME_FINISH    0x00000200
#define M_STATUS_NEDDLE_CLASH   0x00000100
#define M_STATUS_LEFT_TRIG		0x00000080
#define M_STATUS_RIGHT_TRIG		0x00000040
#define M_STATUS_IN_POSITION	0x00000020
#define M_STATUS_IN_SPEED		0x00000010
#define M_STATUS_STOP			0x00000008
#define M_STATUS_STALL			0x00000004
#define M_STATUS_ERR			0x00000002
#define M_STATUS_HOMING			0x00000001

/*
*********************************************************************************************************
*                                              枚举和结构体类型定义
*********************************************************************************************************
*/
typedef enum BOARD_REG{
	BOARD_REG_STATUS = 0x40,
	BOARD_REG_ERR    = 0x41,
}board_reg_t;

typedef enum MOTOR_REG_T{
	M_REG_TYPE 				= 0x01,  /*只读 驱动板类型*/
	M_REG_CURREMT_MAX = 0x02,  /*只读 最大电流*/
	M_REG_INIT_STATUS = 0x03,  /*只读*/
	M_REG_HOMING		  = 0x04,  /*只读 回原点状态*/
	
	M_REG_CURRENT			= 0x20,  /**/
	M_REG_MODE				= 0x21,  /**/
	M_REG_HOME_ACC		= 0x22,  /**/
	M_REG_HOME_SPEED  = 0x23,  /**/
	M_REG_HOME_MODE   = 0x24,  /**/
	M_REG_START_SPEED = 0x25,  /**/
	M_REG_ACC_A1      = 0x26,  /**/
	M_REG_ACC_V1      = 0x27,  /**/
	M_REG_ACC_MAX     = 0x28,  /**/
	M_REG_SPEED       = 0x29,  /**/

	M_REG_DEC_MAX     = 0x2A,  /**/
	M_REG_DEC_D1      = 0x2B,  /**/
	M_REG_STOP_SPEED  = 0x2C,  /**/
	M_REG_POS         = 0x2D,  /**/
	M_REG_TAR_POS     = 0x2E,  /**/
	M_REG_GLOBAL      = 0x2F,  /**/
	M_REG_ERR         = 0x30,  /**/
	M_REG_SURFACE     = 0x31,  /**/
	M_REG_STATUS      = 0x32,  /*只适用在位置模式下 在复位和速度模式中不能以此作为判断依据*/
	M_REG_SURFACE_INIT		= 0x33,	/*只使用在闭环控制垂直电机,连接了液面探测板*/
	M_REG_SURFACE_STATUS	= 0x34,
	M_REG_SURFACE_ENABLE	= 0x35,
	M_REG_HALF_CIRCLE			= 0x36,
	M_REG_MOTION_CURVE = 0x37, /*运动加速减速挡位调整*/
	M_REG_HALT = 0x38,
	M_REG_DISABLE = 0x39,	/*电机去使能,需要重启才能恢复*/
	M_REG_ENC_POS = 0x40,	/*电机去使能 读取当前的编码器位置（计算了LATCH偏移的位置）*/
	M_REG_TCM4361_READ		= 0x41,	/*读取TMC寄存器功能 调试用*/
	M_REG_TCM4361_WRITE		= 0x42,	/*写TMC寄存器功能 调试用*/
	M_LOG = 0xFF,
}motor_reg_t;

typedef enum MOTOR_MODE_T{
	M_MODE_POS 		= 0x00,
	M_MODE_SPEED	= 0x01,
	M_MODE_SPEED_RV	= 0x02,
}motor_mode_t;

#pragma pack(1)
typedef struct{
	uint8_t	sdo_cs;
	uint8_t	sdo_addr;
	uint8_t regaddr;
	int32_t data;
}moto_data_t;
typedef struct{
	uint8_t	sdo_cs;
	uint8_t	sdo_addr;
	uint8_t regaddr;
	uint8_t tmc_addr;
	int32_t reg_data;
}debug_write_t;
#pragma pack()

/*
*********************************************************************************************************
*                                              	全局变量声明
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	函数声明
*********************************************************************************************************
*/
void M_STATUS_SET(uint32_t* status,uint32_t flag);
void M_STATUS_RESET(uint32_t* status,uint32_t flag);

uint8_t motor_read_ack(uint16_t can_id,uint8_t index,motor_reg_t reg,int data,can_msg_t* p_msg);
uint8_t motor_write_ack(uint16_t can_id,uint8_t index,motor_reg_t reg,int data,can_msg_t* p_msg);
#endif
